import React from 'react'
// ===================================================================== Temp
import Controls from '#cpt/temp/Controls'
// ===================================================================== declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Form 		= $Lazy.hook(()=>import('@antd/form/layout/form'))
const FormItem 	= $Lazy.load(()=>import('#cpt/form/submit-item'))
const Box 		= $Lazy.load(()=>import('#tp/box/box'))
const Submit 	= $Lazy.file(()=>import('#cpt/temp/js/onSubmit'))
const IsUpload 	= $Lazy.load(()=>import('./cpt/is-upload'))
// ===================================================================== component
// ===================================================================== component
const Index = ({ rows, mode, result, disabled, p, isFirst, info={}, submitLoading, onRefresh, onRefreshTable }) => {
	const formRef = React.useRef()
	
	React.useEffect( () => {
		setTimeout(()=>{
			formRef.current?.refs.ref?.reset()
			if(formRef.current){
				if($fn.hasObject(result)){
					formRef.current.refs.ref?.setValue(result)
				}
				const result_suggestion_name = result?.result_suggestion_name ? result?.result_suggestion_name : info?.result_suggestion_content
				
				formRef.current.refs.ref?.setValue({
					result_suggestion_name,
					ts_user_name : info?.ts_user_name,
					ts_audit_name : info?.ts_audit_name,
					ts_approve_name : info?.ts_approve_name,
					ts_user_at : info?.ts_user_at,
					ts_audit_at : info?.ts_audit_at,
					ts_approve_at : info?.ts_approve_at,
				})
			}
		})
	},[result, info])
	
	// 操作按钮
	const controls = React.useCallback(() => {
		return [
			Controls.click('保存', () => { formRef.current.refs.ref.submit() },{
				disabled,
				code:'F2',
				// tip: () => $fn.hasArray(rows) ? true : '请先选择项目列表一条数据'
			})
		]
	},[disabled])
	
	const onSubmit = React.useCallback( param => {
		const _rows = $fn.getSelectedRows(rows)
		Submit.then(f => {
			f({
				msg:'保存',
				api:'result-unit-item/update',
				param:{ uuid: _rows?.uuid, dmodel:mode, content:param },
				onRefresh: onRefreshTable
			})
		})
	},[ rows, mode, onRefreshTable ])
	
	return (
		<Box className='h' flex scroll full title='图形与解释' scrollClassName='plr10 pb10' style={{width:280}} controls={controls()} loading={submitLoading}>
			<Form onSubmit={onSubmit} className='submit-form small-form' ref={formRef}>
				{
					(mode === 44014 || mode === 44027 || mode === 44028 || mode === 44017 || mode === 44025 || mode === 44019) && (
						<>
							<>
								{
									+info?.kd_check_result_cats?.result2 === 1 && <FormItem title='结果2' name='result2' disabled={disabled} />
								}
								{
									+info?.kd_check_result_cats?.result3 === 1 && <FormItem title='结果3' name='result3' disabled={disabled} />
								}
							</>
						</>
					)
				}
				{
					(mode === 44014 || mode === 44027 || mode === 44028 || mode === 44017 || mode === 44025) && <FormItem title='建议与解释（手动）' name='result_suggestion_name' mode='textarea' disabled={disabled} form={formRef} search={{ i:1, p:{type:2, kind_id: info?.kind_category === '25002' ? info?.parent_kind_id : info?.kind_id } }}/>
				}
				{
					(mode === 44014 || mode === 44027 || mode === 44028 || mode === 44017 || mode === 44025) &&  <FormItem title='临床建议' name='clinical_advice' mode='textarea' disabled={disabled} />
				}
				<IsUpload info={info} disabled={disabled} multiple r={false} />
				{/* 临床诊断下面增加检验者，审核者，批准者，检验时间，审核时间，批准时间 2021-09-26  --耿琰 */}
				{
					(mode === 44014 || mode === 44027 || mode === 44028 || mode === 44017 || mode === 44025) &&  <FormItem title='检验人' name='ts_user_name' disabled={true} p='' />
				}
				{
					(mode === 44014 || mode === 44027 || mode === 44028 || mode === 44017 || mode === 44025) &&  <FormItem title='审核人' name='ts_audit_name' disabled={true} p='' />
				}
				{
					(mode === 44014 || mode === 44027 || mode === 44028 || mode === 44017 || mode === 44025) &&  <FormItem title='批准人' name='ts_approve_name' disabled={true} p='' />
				}
				{
					(mode === 44014 || mode === 44027 || mode === 44028 || mode === 44017 || mode === 44025) &&  <FormItem title='检验时间' name='ts_user_at'  type='date-time' disabled={true} p='' />
				}
				{
					(mode === 44014 || mode === 44027 || mode === 44028 || mode === 44017 || mode === 44025) &&  <FormItem title='审核时间' name='ts_audit_at' type='date-time' disabled={true} p='' />
				}
				{
					(mode === 44014 || mode === 44027 || mode === 44028 || mode === 44017 || mode === 44025) &&  <FormItem title='批准时间' name='ts_approve_at' type='date-time' disabled={true} p='' />
				}
				{/* 44019，44024临床诊断下面增加检验者，审核者，批准者，检验时间，审核时间，批准时间 2021-09-28  --耿琰 */}
				{
					mode === 44019 && (
						<>
							<FormItem title='建议与解释（手动）' name='result_suggestion_name' mode='textarea' disabled={disabled}  form={formRef} search={{ i:1, p:{type:1, kind_id: info?.kind_category === '25002' ? info?.parent_kind_id : info?.kind_id } }}/>
							<FormItem title='结果' name='result' mode='textarea' disabled={disabled} />
							<FormItem title='检验人' name='ts_user_name' disabled={true} p='' />
							<FormItem title='审核人' name='ts_audit_name' disabled={true} p='' />
							<FormItem title='批准人' name='ts_approve_name' disabled={true} p='' />
							<FormItem title='检验时间' name='ts_user_at'  type='date-time' disabled={true} p='' />
							<FormItem title='审核时间' name='ts_audit_at' type='date-time' disabled={true} p='' />
							<FormItem title='批准时间' name='ts_approve_at' type='date-time' disabled={true} p='' />
						</>
					)
				}
				{
					mode === 44024 && (
						<>
							<FormItem title='解释' name='result_suggestion_name' mode='textarea' disabled={disabled} form={formRef} search={{ i:1, p:{type:1, kind_id: info?.kind_category === '25002' ? info?.parent_kind_id : info?.kind_id} }}/>
							<FormItem title='结果' name='result' mode='textarea' disabled={disabled} form={formRef} search={{ i:1, p:{type:0, kind_id: info?.kind_category === '25002' ? info?.parent_kind_id : info?.kind_id} }}/>
							<FormItem title='检验人' name='ts_user_name' disabled={true} p='' />
							<FormItem title='审核人' name='ts_audit_name' disabled={true} p='' />
							<FormItem title='批准人' name='ts_approve_name' disabled={true} p='' />
							<FormItem title='检验时间' name='ts_user_at'  type='date-time' disabled={true} p='' />
							<FormItem title='审核时间' name='ts_audit_at' type='date-time' disabled={true} p='' />
							<FormItem title='批准时间' name='ts_approve_at' type='date-time' disabled={true} p='' />
						</>
					)
				}
			</Form>
		</Box>
	)
}

export default Index